package com.activititest.service.impl;

import com.activititest.entity.EGroup;
import com.activititest.entity.EUser;
import com.activititest.service.CommonServer;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
public class CommonServerImpl implements CommonServer {
    @Resource
    private IdentityService identityService;
    @Override
    public boolean addGroup(EGroup eGroup) {
//        String s = UUID.randomUUID().toString();
        List<Group> list = identityService.createGroupQuery().groupName(eGroup.getGroupname()).list();
        for(Group gg: list){
            identityService.deleteGroup(gg.getId());
        }

        Group group = identityService.newGroup(eGroup.getGroupid());
        group.setName(eGroup.getGroupname());
        group.setType(eGroup.getGrouptype());
        identityService.saveGroup(group);
        return true;
    }

    @Override
    public boolean addUser(EUser eUser) {
        List<User> list1 = identityService.createUserQuery().userLastName(eUser.getUsername()).list();

        for(User gg: list1){
            identityService.deleteUser(gg.getId());
        }
        String s = UUID.randomUUID().toString();
        User user = identityService.newUser(s);
        user.setLastName(eUser.getUsername());
        user.setPassword(eUser.getPassword());
        identityService.saveUser(user);
        Group group = identityService.createGroupQuery().groupName(eUser.getUsergroup()).singleResult();
        if(group==null){
            System.out.println("没有找到组");
            return false;
        }
        identityService.createMembership(user.getId(),group.getId());


        return true;
    }

    @Override
    public List<EGroup> findGroups() {
        List<Group> list = identityService.createGroupQuery().list();
        List<EGroup> froups=new ArrayList<>();
        EGroup eGroup = new EGroup();
        for(Group fp:list){
            eGroup = new EGroup();
            eGroup.setGrouptype(fp.getType());
            eGroup.setGroupname(fp.getName());
            froups.add(eGroup);
        }
        return froups;
    }

    @Override
    public List<EUser> findEUser(String groupname) {
        Group group = identityService.createGroupQuery().groupName(groupname).singleResult();
        List<User> usersInGroup = identityService.createUserQuery().memberOfGroup(group.getId()).list();
        System.out.println(usersInGroup.size());
        List<EUser> eusers =new ArrayList<>();
        EUser euser;
        for (User user : usersInGroup) {
            euser=new EUser();
            euser.setUsername(user.getLastName());
            eusers.add(euser);
        }
        return eusers;


    }

    @Override
    public EUser login(String username, String password) {
        EUser eUser = new EUser();
        User user = identityService.createUserQuery().userLastName(username).singleResult();
        if(user==null){
            return null;
        }
        boolean b = identityService.checkPassword(user.getId(), password);
        if(b){
            Group group = identityService.createGroupQuery().groupMember(user.getId()).singleResult();
            eUser.setUsergroup(group.getId());
            eUser.setUsername(user.getLastName());
            eUser.setPassword(password);
            return eUser;

        }
        return null;
    }
}
